package com.yoso.thread;

import cn.hutool.core.thread.ThreadUtil;
import com.yoso.config.thread.ThreadPoolFactory;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class ThreadPool {
    private static final AtomicInteger num = new AtomicInteger(1);
    private
    static class YosoThread extends Thread {
        private final int id;
        public YosoThread(int i) {
            id = i;
        }

        @Override
        public void run() {
            ThreadUtil.sleep(300);
            log.info("YosoThread.run---"+"当前线程id: " +id+" ---执行次数" + num.getAndIncrement());
        }
    }

    @Test
    public void threadPool(){
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5, 10, 20, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1),
                new ThreadPoolFactory(), new ThreadPoolExecutor.DiscardPolicy());
        int count = 20;
        for (int i = 0; i < count; i++) {

            threadPoolExecutor.execute(new YosoThread(i + 1));
        }

        ThreadUtil.sleep(7000);
    }
}
